package com.example.demo.oo.domain;

import com.example.demo.oo.domain.vo.Mobile;
import lombok.Data;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Data
public class Captcha {


    @NotNull(message = "短信验证码不能为空")
    @Size(min = 4, max = 4, message = "短信验证码只能四位")
    private String value;

    @Valid
    private Mobile mobile;

    // 过期时间
    private static long expire = 60;

    private String content = "%s为您的登录验证码，请于%s分钟内填写，如非本人操作，请忽略本短信。";

    public void generate() {

        // 生成随机四位数字
        generateValue();

        // 生成短信内容
        generateContent();
    }

    // 生成随机四位数字
    private void generateValue() {
        value = String.valueOf((int)(Math.random() * 9000 + 1000));
    }

    // 生成短信内容
    private void generateContent() {
        long minute = expire / 60;
        content = String.format(content, value, minute);
    }

    public static long getExpire() {
        return expire;
    }

    public String cacheKey() {
        return mobile.getValue();
    }


}
